package luanluan.control.buttons 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	/**
	 * 按钮基类
	 * @author luanluan
	 */
	public class BaseButton 
	{
		private var _mc:MovieClip;				//	*制作按钮的MovieClip
		protected var _enable:Boolean;			//	*是否启用，即是否添加鼠标事件
		
		//	*鼠标事件
		private var _clickFunction:Function = null;	
		private var _downFunction:Function = null;
		private var _overFunction:Function = null;
		
		//	*帧
		protected const FLAG_UP:int = 1;
		protected const FLAG_OVER:int = 2;
		protected const FLAG_DOWN:int = 3;
		
		/**
		 * 传递一个MovieClip,制作成按钮功能的东东
		 * @param	mc		一个具有三帧的MovieClip(分别为弹起，鼠标经过，鼠标按下状态)
		 */
		public function BaseButton(mc:MovieClip) 
		{
			_mc = mc;
			_mc.gotoAndStop(FLAG_UP);
			enable = false;
		}
		
		/**
		 * 添加侦听器
		 */
		protected function  addListener():void
		{
			_mc.mouseChildren = _mc.mouseEnabled = true;
			_mc.addEventListener(MouseEvent.ROLL_OVER, mouseOverHandle);
			_mc.buttonMode = true;
		}
		
		/**
		 * 移除侦听器
		 */
		protected function removeListener():void
		{
			_mc.mouseChildren = _mc.mouseEnabled = false;
			_mc.removeEventListener(MouseEvent.ROLL_OVER, mouseOverHandle);
			_mc.removeEventListener(MouseEvent.ROLL_OUT, mouseOutHandle);
			_mc.removeEventListener(MouseEvent.CLICK, mouseClickHandle);
			_mc.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandle);
			_mc.buttonMode = false;
		}
		
		protected function mouseOverHandle(e:MouseEvent):void 
		{
			_mc.gotoAndStop(FLAG_OVER);
			
			if (_overFunction != null)
				_overFunction(e);
				
			_mc.addEventListener(MouseEvent.ROLL_OUT, mouseOutHandle);
			_mc.addEventListener(MouseEvent.CLICK, mouseClickHandle);
			_mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandle);
		}
		
		protected function mouseClickHandle(e:MouseEvent):void 
		{
			if (_clickFunction != null)
				_clickFunction(e);
				
		}
		
		protected function mouseDownHandle(e:MouseEvent):void 
		{
			_mc.gotoAndStop(FLAG_DOWN);
			
			if (_downFunction != null)
				_downFunction(e);
				
			_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandle);
		}
		
		protected function mouseUpHandle(e:MouseEvent):void 
		{
			_mc.gotoAndStop(FLAG_OVER);
			
			_mc.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandle);
		}
		
		protected function mouseOutHandle(e:MouseEvent):void 
		{
			_mc.gotoAndStop(FLAG_UP);
			
			_mc.removeEventListener(MouseEvent.ROLL_OUT, mouseOutHandle);
			_mc.removeEventListener(MouseEvent.CLICK, mouseClickHandle);
			_mc.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandle);
		}
		
		public function get mc():MovieClip 			{			return _mc;		}
		
		public function get enable():Boolean 		{			return _enable;		}
		
		//	*设置是否可用
		public function set enable(value:Boolean):void 
		{
			if (_enable == value)
				return;
				
			if (_enable)
				addListener();
			else
				removeListener();
		}
		
		//	* 鼠标点击执行的操作
		public function set onMouseClick(value:Function ):void
		{
			_clickFunction = value;
		}
		
		//	* 鼠标经过执行的操作
		public function set onMouseOver(value:Function ):void
		{
			_overFunction = value;
		}
		
		//	* 鼠标按下执行的操作
		public function set onMouseDown(value:Function ):void
		{
			_downFunction = value;
		}
		
	}

}